package com.of.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.of.mapper.OrdersMapper;
import com.of.pojo.BasePojo;
import com.of.pojo.Orders;

import com.of.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService{
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public PageResult findOrdersList(PageResult pageResult) {
        //1.总记录数
        long total = ordersMapper.findTotal();
        //2.获取分页查询的结果
        int start = (pageResult.getPageNum() - 1) * pageResult.getPageSize();
        int size = pageResult.getPageSize();
        String query = pageResult.getQuery();
        List<Orders> rows = ordersMapper.findOrdersList(start,size,query);
        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    @Transactional
    public void deleteOrdersById(Integer id) {
        ordersMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void saveOrders(Orders orders) {
         orders.setId(null)
                .setUsername(orders.getUsername())
                .setPrice(orders.getPrice())
                .setNumber(orders.getNumber())
                .setMoney(orders.getPrice() * orders.getPrice())
                .setCreated(new Date());
        ordersMapper.insert(orders);
    }

    @Override
    public Orders findOrdersById(Integer id) {
        return ordersMapper.selectById(id);
    }

    @Override
    @Transactional
    public void updateOrders(Orders orders) {
        orders.setUpdated(new Date());
        Orders orders1 = orders.setId(orders.getId());
        ordersMapper.updateById(orders1);
    }
}
